package com.bestescortbedsystem.service.impl;

import com.bestescortbedsystem.dao.SesAddressDao;
import com.bestescortbedsystem.dao.SesHospitalinfoDao;
import com.bestescortbedsystem.dao.SesUserDao;
import com.bestescortbedsystem.entity.*;
import com.bestescortbedsystem.service.HospitalininfoService;
import com.bestescortbedsystem.util.FindDate;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: HospitalininfoServiceImpl
 * Package: com.bestescortbedsystem.service.impl
 *
 * @author: guanzepeng
 * @Date: 2021/3/3 09:44 星期三
 * @Description: 医院业务层接口
 */
@Service
public class HospitalininfoServiceImpl implements HospitalininfoService {

	@Resource
	private SesHospitalinfoDao sesHospitalinfoDao;

	@Resource
	private SesUserDao sesUserDao;

	@Resource
	private SesAddressDao sesAddressDao;


	/**
	 * 带分页的医院数据展示,可以根据条件查询
	 * @param sesHospitalinfo  传入查询条件
	 * @param pageNum 从第几条数据开始
	 * @param pageSize 显示几条数据
	 * @return 返回Map
	 */
	@Override
	public Map<String, Object> findByHosp(SesHospitalinfo sesHospitalinfo, Integer pageNum, Integer pageSize) {
		SesHospitalinfoExample sesHospitalinfoExample = new SesHospitalinfoExample();
		SesHospitalinfoExample.Criteria criteria = sesHospitalinfoExample.createCriteria();
		Map<String, Object> map = new HashMap<>();
		//医院名称
		if(sesHospitalinfo.getHospitalinfoname() != null) {
			criteria.andHospitalinfonameLike("%" + sesHospitalinfo.getHospitalinfoname() + "%");
		}
		//省市县
		Long sheng = sesHospitalinfo.getHospitalinfoprovinceid();
		Long shi = sesHospitalinfo.getHospitalinfocityid();
		Long xian = sesHospitalinfo.getHospitalinfocontyid();
		if(sheng != null) {
			criteria.andHospitalinfoprovinceidEqualTo(sheng);
		}
		if(shi != null) {
			criteria.andHospitalinfocityidEqualTo(shi);
		}
		if(xian != null) {
			criteria.andHospitalinfocontyidEqualTo(xian);
		}
		//股东
		if(sesHospitalinfo.getUsershareholderid() != null) {
			criteria.andUsershareholderidEqualTo(sesHospitalinfo.getUsershareholderid());
		}
		//收益分成
		if(sesHospitalinfo.getHospitalinfosharing() != null) {
			criteria.andHospitalinfosharingEqualTo(sesHospitalinfo.getHospitalinfosharing());
		}

		//查询所有的医院
		PageHelper.startPage(pageNum, pageSize);
		List<SesHospitalinfo> sesHospitalinfoList = sesHospitalinfoDao.selectByExample(sesHospitalinfoExample);
		for (SesHospitalinfo s : sesHospitalinfoList) {
			SesUser user = sesUserDao.selectByPrimaryKey(s.getUserid());
			s.setDuijieren(user);
		}
		PageInfo<SesHospitalinfo> pageInfo = new PageInfo<>(sesHospitalinfoList);
		map.put("yiyuan",pageInfo);

		//查询所有的股东
		SesUserExample sesUserExample = new SesUserExample();
		sesUserExample.createCriteria().andUsertypeEqualTo(4);
		List<SesUser> sesUserList = sesUserDao.selectByExample(sesUserExample);
		map.put("suoyougudong", sesUserList);

		//查询所有一级分销商
		SesUserExample sesUserExample1 = new SesUserExample();
		sesUserExample1.createCriteria().andUsertypeEqualTo(10);
		List<SesUser> sesUserList1 = sesUserDao.selectByExample(sesUserExample1);
		map.put("yiji", sesUserList1);

		//查询所有二级分销商
		SesUserExample sesUserExample2 = new SesUserExample();
		sesUserExample2.createCriteria().andUsertypeEqualTo(11);
		List<SesUser> sesUserList2 = sesUserDao.selectByExample(sesUserExample2);
		map.put("erji", sesUserList2);

		//查询所有的医院对接人
		SesUserExample sesUserExample3 = new SesUserExample();
		sesUserExample3.createCriteria().andUsertypeEqualTo(3);
		List<SesUser> sesUserList3 = sesUserDao.selectByExample(sesUserExample3);
		map.put("duijieren", sesUserList3);

		return map;
	}

	/**
	 * 查询省
	 * @return 返回lis
	 */
	@Override
	public List<SesAddress> findProvince() {
		List<SesAddress> province = sesAddressDao.findProvince();
		return province;
	}

	/**
	 * 查询市
	 * @param id 传入id
	 * @return 返回List
	 */
	@Override
	public List<SesAddress> findCity(Integer id) {
		if(id == null || id == 0) {
			List<SesAddress> city = sesAddressDao.findCity(11);
			return city;
		}
		List<SesAddress> city = sesAddressDao.findCity(id);
		return city;
	}

	/**
	 * 查询县区
	 * @param id 传入id
	 * @return 返回List
	 */
	@Override
	public List<SesAddress> findCounty(Integer id) {
		if(id == null || id == 0) {
			List<SesAddress> county = sesAddressDao.findCounty(1101);
			return county;
		}
		List<SesAddress> county = sesAddressDao.findCounty(id);
		return county;
	}

	/**
	 * 删除医院
	 * @param hospitalinfoid 传入医院ID
	 * @return 返回Boolean
	 */
	@Override
	public Boolean deleteHosp(Long hospitalinfoid) {
		if(hospitalinfoid == null) {
			return false;
		}
		boolean b = sesHospitalinfoDao.deleteByPrimaryKey(hospitalinfoid) == 1;
		return b;
	}

	/**
	 * 新增医院
	 * @param sesHospitalinfo 传入医院对象
	 * @return 返回Boolean
	 */
	@Override
	public Boolean insertHosp(SesHospitalinfo sesHospitalinfo) {
		if(sesHospitalinfo == null) {
			return false;
		}
		sesHospitalinfo.setHospitalinfocreatetime(FindDate.getDate());
		boolean b = sesHospitalinfoDao.insertSelective(sesHospitalinfo) == 1;
		return b;
	}
}
